package news.dao;

import news.example.User;
import news.util.BaseDao;

import java.util.ArrayList;
import java.util.List;

public class QueryUserDaoImpl implements IQueryUserDao{
    BaseDao baseDao = new BaseDao();
    @Override
    public List<User> findByName(String username) {
        List<User> list = null;
        try {
            baseDao.openDB();
            String sql = "select * from news_users where username = ?";
            baseDao.ps = baseDao.conn.prepareStatement(sql);
            baseDao.ps.setString(1,username);
            baseDao.rs = baseDao.ps.executeQuery();

            list = new ArrayList<User>();
            while (baseDao.rs.next()){
                User user = new User();
                user.setUserId(baseDao.rs.getInt(1));
                user.setUserName(baseDao.rs.getString(2));
                user.setPassword(baseDao.rs.getString(3));
                user.setFullName(baseDao.rs.getString(4));
                user.setType(baseDao.rs.getString(5));
                list.add(user);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int countByName(String username) {
        int count = 0;
        try {
            baseDao.openDB();
            String sql = "select count(*) from news_users where username = ?";
            baseDao.ps = baseDao.conn.prepareStatement(sql);
            baseDao.ps.setString(1,username);
            baseDao.rs = baseDao.ps.executeQuery();

            while (baseDao.rs.next()){
                count = baseDao.rs.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }
}
